/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.group.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.xiaonuo.auth.core.pojo.SaBaseLoginUser;
import vip.xiaonuo.auth.core.util.StpLoginUserUtil;
import vip.xiaonuo.biz.modular.group.entity.BizGroup;
import vip.xiaonuo.biz.modular.group.entity.BizGroupUser;
import vip.xiaonuo.biz.modular.group.param.*;
import vip.xiaonuo.biz.modular.group.service.BizGroupService;
import vip.xiaonuo.biz.modular.group.service.BizGroupUserService;
import vip.xiaonuo.biz.modular.user.entity.BizUser;
import vip.xiaonuo.biz.modular.user.service.BizUserService;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;

import java.util.List;

/**
 * bizGroup控制器
 *
 * @author xsl
 * @date  2024/06/28 14:23
 */
@Tag(name = "bizGroup控制器")
@RestController
@Validated
public class BizGroupController {

    private static Logger logger = LoggerFactory.getLogger(BizGroupController.class);

    @Resource
    private BizGroupService bizGroupService;

    @Resource
    private BizGroupUserService bizUserGroupService;

    @Resource
    private BizUserService bizUserService;

    /**
     * 获取bizGroup分页
     * @author xsl
     * @date  2024/06/28 14:23
     */
    @Operation(summary = "获取bizGroup分页")
    @SaCheckPermission("/biz/group/page")
    @GetMapping("/biz/group/page")
    public CommonResult<Page<BizGroup>> page(BizGroupPageParam bizGroupPageParam) {
        return CommonResult.data(bizGroupService.page(bizGroupPageParam));
    }

    /**
     * 获取bizGroup分页
     * @author xsl
     * @date  2024/06/28 14:23
     */
    @Operation(summary = "获取bizGroup分页")
    @SaCheckPermission("/biz/group/page")
    @GetMapping("/biz/group/myGroup")
    public CommonResult<Page<BizGroupUser>> myGroup(BizGroupUserPageParam bizGroupUserPageParam) {
        return CommonResult.data(bizUserGroupService.myGroup(bizGroupUserPageParam));
    }

    /**
     * 添加bizGroup
     * @author xsl
     * @date  2024/06/28 14:23
     */
    @Operation(summary = "添加bizGroup")
    @CommonLog("添加bizGroup")
    @SaCheckPermission("/biz/group/add")
    @PostMapping("/biz/group/add")
    public CommonResult<String> add(@RequestBody @Valid BizGroupAddParam bizGroupAddParam) {
        bizGroupService.add(bizGroupAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑bizGroup
     * @author xsl
     * @date  2024/06/28 14:23
     */
    @Operation(summary = "编辑bizGroup")
    @CommonLog("编辑bizGroup")
    @SaCheckPermission("/biz/group/edit")
    @PostMapping("/biz/group/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizGroupEditParam bizGroupEditParam) {
        bizGroupService.edit(bizGroupEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除bizGroup
     * @author xsl
     * @date  2024/06/28 14:23
     */
    @Operation(summary = "删除bizGroup")
    @CommonLog("删除bizGroup")
    @SaCheckPermission("/biz/group/delete")
    @PostMapping("/biz/group/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")  List<BizGroupIdParam> bizGroupIdParamList) {
        bizGroupService.delete(bizGroupIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取bizGroup详情
     * @author xsl
     * @date  2024/06/28 14:23
     */
    @Operation(summary = "获取bizGroup详情")
    @SaCheckPermission("/biz/group/detail")
    @GetMapping("/biz/group/detail")
    public CommonResult<BizGroup> detail(@Valid BizGroupIdParam bizGroupIdParam) {
        return CommonResult.data(bizGroupService.detail(bizGroupIdParam));
    }

    /**
     * 加入社团
     * @author xsl
     * @date  2024/06/28 14:23
     */
    @Operation(summary = "加入社团")
    @SaCheckLogin
    @GetMapping("/biz/group/join")
    public CommonResult<String> join( @RequestParam(name = "userId" ) String userId, @RequestParam(name = "groupId" ) String groupId) {
        logger.info("用户Id为：{}的用户请求加入社团id为：{}的社团",userId,groupId);
        BizUser user = bizUserService.getById(userId);
        BizGroup group = bizGroupService.getById(groupId);
        bizUserGroupService.joinGroup(user, group);
        return CommonResult.ok();
    }

    /**
     * 通过社团加入申请
     * @author xsl
     * @date  2024/06/28 14:23
     */
    @Operation(summary = "通过社团加入申请")
    @CommonLog("通过社团加入申请")
    @SaCheckPermission("/biz/groupApply/apply")
    @GetMapping("/biz/groupApply/apply")
    public CommonResult<String> apply( @RequestParam(name = "applyId" ) String applyId, @RequestParam(name = "method" ) String method) {
        logger.info("处理申请id：{}申请请求，该请求为:{}",applyId, method);
        bizUserGroupService.applyRequest(applyId, method);
        return CommonResult.ok();
    }

    /**
     * 获取社团申请表分页
     * @author 薛守立
     * @date  2024/07/03 08:49
     */
    @Operation(summary = "获取社团申请表分页")
    @SaCheckPermission("/biz/groupApply/page")
    @GetMapping("/biz/groupApply/page")
    public CommonResult<Page<BizGroupUser>> page(BizGroupUserPageParam bizUserGroupPageParam) {
        return CommonResult.data(bizUserGroupService.page(bizUserGroupPageParam));
    }

    /**
     * 获取管理社团的列表
     * @author 薛守立
     * @date  2024/07/03 08:49
     */
    @Operation(summary = "获取我的社团")
    @GetMapping("/biz/groupApply/getGroupData")
    public CommonResult<List<BizGroupUser>> getGroupData(BizGroupUserPageParam bizUserGroupPageParam) {
        // 获取我创建的或者我加入的社团
        SaBaseLoginUser user = StpLoginUserUtil.getLoginUser();
        logger.info("处理申请id：{}申请请求",user.getId());
        List<BizGroupUser> res = bizUserGroupService.getGroupList(user.getId());
        return CommonResult.data(res);
    }

    /**
     * 获取管理社团的成员信息
     * @author 薛守立
     * @date  2024/07/03 08:49
     */
    @Operation(summary = "获取管理社团的成员信息")
    @GetMapping("/biz/group/getUserList")
    public CommonResult<List<BizGroupUserVoParam>> getUserList(@RequestParam(name = "groupId" ) String groupId) {
        List<BizGroupUserVoParam> res = bizUserGroupService.getUserList(groupId);
        return CommonResult.data(res);
    }

    /**
     * 获取我的社团信息
     * @author 薛守立
     * @date  2024/07/03 08:49
     */
    @Operation(summary = "获取管理社团的成员信息")
    @GetMapping("/biz/groupApply/getMyApply")
    public CommonResult<Page<BizGroupUser>> getMyApply(BizGroupUserPageParam bizGroupUserPageParam) {
        Page<BizGroupUser> res = bizUserGroupService.getMyApply(bizGroupUserPageParam);
        return CommonResult.data(res);
    }


    /**
     * 删除申请信息
     * @author xsl
     * @date  2024/06/28 14:23
     */
    @Operation(summary = "删除申请信息")
    @CommonLog("删除申请信息")
    @PostMapping("/biz/groupApply/back")
    public CommonResult<String> back(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                       List<BizGroupUserIdParam> bizGroupUserIdParamList) {
        bizUserGroupService.delete(bizGroupUserIdParamList);
        return CommonResult.ok();
    }

}
